package com.linklee.Chapter3;

import com.linklee.Chapter3.entity.Artist;
import com.linklee.Chapter3.entity.User;
import com.linklee.Chapter3.entity.UserCollections;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Chapter4Learn {

    public static void main(String[] args) {

        //chapter4-4
        // object -> 基本类型的转换
        User xiaoMing = new User("1", "小明", 3, "233");

        User xiaoLi = new User("2", "小丽", 4, "2334");

        User xiaoWang = new User("3", "小王", 6, "233334");

        User xiaoZhang = new User("4", "小张", 7, "25577567");

        List<User> list = new ArrayList<>();
        list.add(xiaoMing);
        list.add(xiaoLi);
        list.add(xiaoWang);
        list.add(xiaoZhang);

        LongSummaryStatistics summaryStatistics =
                list.stream().mapToLong(map -> map.getAge()).summaryStatistics();

        System.out.println(summaryStatistics.getAverage());
        System.out.println(summaryStatistics.getMin());
        System.out.println(summaryStatistics.getSum());
        System.out.println(summaryStatistics.getCount());

        // chapter 4.9
        // lambda表达式的重载问题
        // 如果两个接口的入参一样,则选择更具体的类型(子类)。
        // 如果两个接口的入参一样,还无更具体的继承关系,如果不强转编译时候就会报错。



        // chapter 4-12
        // 默认方法的权衡
        // 1.类胜于接口,如果在继承链中有方法体或抽象方法的声明就可以忽略接口的默认方法
        // 2.子类胜于父类:这个类似于继承部分的,如果父子定义了同一个方法,则子类的胜出。

        // chapter 4-13
        // 用来判定空的 optional

        Optional<User> optional1 = Optional.of(xiaoWang);
        // 判断对象是否为空 空返回false
        System.out.println(optional1.isPresent());
        User op1 = optional1.orElse(xiaoLi);
        User oa1 = optional1.orElseGet(() -> xiaoLi);

        Optional<User> optional = Optional.ofNullable(null);
        Optional<User> optional2 = Optional.empty();
        // ofnullable 与 empty 都会返回一个空的optional
        System.out.println(optional.isPresent());
        System.out.println(optional2.isPresent());
        User op = optional.orElse(xiaoLi);
        User oa = optional.orElseGet(() -> xiaoLi);

        System.out.println(op);
        System.out.println(oa);
        System.out.println(op1);
        System.out.println(oa1);


        // chapter 4-26
        Artist artist = new Artist("1","1");
        Artist artist1 = new Artist("2","2");
        Artist artist2 = new Artist("3","3");
        Artist artist3 = new Artist("4","4");
        Artist artist4 = new Artist("5","5");
        Artist artist5 = new Artist("6","6");

        List<Artist> artists1 = new ArrayList<>();
        artists1.add(artist);
        artists1.add(artist1);
        artists1.add(artist2);

        List<Artist> artists2 = new ArrayList<>();
        artists2.add(artist3);
        artists2.add(artist4);
        artists2.add(artist5);

        Artist artistList = new Artist("a1",artists1,"a1");
        Artist artistList1 = new Artist("a2",artists2,"a2");


        List<Artist> artists3 =  Stream.of(artistList,artistList1).collect(Collectors.toList());

       List a =  artists3.stream().flatMap(artistsObj->Stream.concat(Stream.of(artistsObj),artistsObj.getMembers())).collect(Collectors.toList());

    }
}
